﻿using System.Linq.Expressions;
using Basic.FormDAL;
using Basic.FormModel.DB;
using Basic.FormRemoteModel;
using Basic.FormRemoteModel.Form.Model;
using SqlSugar;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Basic.FormCollect.lmpl
{
    internal class CustomFormCollect : ICustomFormCollect
    {
        private readonly ICustomFormDAL _FormDAL;

        public CustomFormCollect ( ICustomFormDAL formDAL )
        {
            this._FormDAL = formDAL;
        }

        public Result[] Gets<Result> ( long[] ids ) where Result : class
        {
            return this._FormDAL.Gets<Result>(ids);
        }
        public Result[] Gets<Result> ( long[] ids, Expression<Func<DBCustomForm, Result>> selector )
        {
            return this._FormDAL.Gets<Result>(ids, selector);
        }
        public DBCustomForm Add ( FormAdd data )
        {
            if ( this._FormDAL.IsExists(a => a.FormClassify == data.FormClassify && a.FormName == data.FormName) )
            {
                throw new ErrorException("form.name.repeat");
            }
            return this._FormDAL.Add(data);
        }
        public Result[] Query<Result> ( FormQuery query, IBasicPage paging, out int count ) where Result : class
        {
            return this._FormDAL.Query<Result>(query, paging, out count);
        }
        public Result Get<Result> ( long formId ) where Result : class
        {
            return this._FormDAL.Get<Result>(formId);
        }
        public Result Get<Result> ( long id, Expression<Func<DBCustomForm, Result>> selector )
        {
            return this._FormDAL.Get(id, selector);
        }
        public DBCustomForm Get ( long id )
        {
            DBCustomForm form = this._FormDAL.Get(id);
            if ( form.IsDrop )
            {
                throw new ErrorException("form.already.drop");
            }
            return form;
        }
        public bool Set ( DBCustomForm source, FormSet set )
        {
            if ( ( set.FormName != source.FormName || source.FormClassify != set.FormClassify ) && this._FormDAL.IsExists(a => a.FormClassify == set.FormClassify && a.FormName == set.FormName) )
            {
                throw new ErrorException("form.name.repeat");
            }
            return this._FormDAL.Update(source, set);
        }
        public void Delete ( DBCustomForm source )
        {
            if ( source.FormStatus == FormStatus.启用 )
            {
                throw new ErrorException("form.not.allow.delete");
            }
            else if ( source.FormStatus == FormStatus.起草 )
            {
                this._FormDAL.Delete(source.Id);
            }
            else
            {
                this._FormDAL.Drop(source.Id);
            }
        }

        public bool SetStatus ( DBCustomForm source, FormStatus status )
        {
            if ( source.FormStatus == status )
            {
                return false;
            }
            this._FormDAL.SetStatus(source, status);
            return true;
        }

        public bool CheckIsUse ( long groupId )
        {
            return this._FormDAL.IsExists(a => a.FormType == FormType.代理表单 && a.PhyGroupId == groupId);
        }

        public Dictionary<long, int> GetPhyGroupUseNum ( long[] groupId )
        {
            return this._FormDAL.GroupBy(a => a.FormType == FormType.代理表单 && groupId.Contains(a.PhyGroupId.Value), a => a.PhyGroupId, a => new
            {
                a.PhyGroupId,
                num = SqlFunc.AggregateCount(a.PhyGroupId)
            }).ToDictionary(a => a.PhyGroupId.Value, a => a.num);
        }

        public bool CheckIsPhysical ( long formId )
        {
            return this._FormDAL.Get(formId, a => a.FormType == FormType.代理表单);
        }

        public Dictionary<long, string> GetName ( long[] ids )
        {
            return this._FormDAL.Gets(ids, a => new
            {
                a.Id,
                a.FormName
            }).ToDictionary(a => a.Id, a => a.FormName);
        }

        public void LockForm ( LockFormArg arg )
        {
            this._FormDAL.LockForm(arg);
        }

        public void SetLayoutType ( DBCustomForm form, FormLayoutType layout )
        {
            if ( form.Layout == layout )
            {
                return;
            }
            this._FormDAL.SetLayoutType(form.Id, layout);
        }
    }
}
